package com.spzx.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.spzx.common.service.exception.SpzxException;
import com.spzx.manager.listener.ExcelListener;
import com.spzx.manager.mapper.CategoryMapper;
import com.spzx.manager.service.CategoryService;
import com.spzx.model.entity.product.Category;
import com.spzx.model.vo.common.ResultCodeEnum;
import com.spzx.model.vo.product.CategoryExcelVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findByParentId(Long parentId) {
        // 根据分类id查询它下面的所有的子分类数据
        List<Category> categoryList = categoryMapper.selectByParentId(parentId);
        if (!CollectionUtils.isEmpty(categoryList)) {

            // 遍历分类的集合，获取每一个分类数据
            categoryList.forEach(item -> {
                // 查询该分类下子分类的数量
                int count = categoryMapper.countByParentId(item.getId());
                item.setHasChildren(count > 0);

            });
        }
        return categoryList;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            // 设置响应结果类型
            response.setContentType("application/vnd.ms-excel");
            // 设置响应结果的编码方式
            response.setCharacterEncoding("utf-8");
            // 设置下载文件名称，并且设置文件的解码方式是utf-8
            String fileName = URLEncoder.encode("分类数据", StandardCharsets.UTF_8);
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 查询商品分类的内容
            List<Category> categoryList = categoryMapper.selectAll();
            // 将数据转化
            List<CategoryExcelVo> categoryExcelVoList = new ArrayList<>(categoryList.size());
            categoryList.forEach(item -> {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(item, categoryExcelVo);
                categoryExcelVoList.add(categoryExcelVo);
            });
            // 写入excel表格中
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("分类数据").doWrite(categoryExcelVoList);
        } catch (IOException e) {
            throw new SpzxException(ResultCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public void importData(MultipartFile file) {
        // 读取
        try {
            ExcelListener<CategoryExcelVo> excelListener = new ExcelListener(categoryMapper);
            EasyExcel.read(file.getInputStream(), CategoryExcelVo.class, excelListener)
                    .sheet().doRead();
        } catch (IOException e) {
            throw new SpzxException(ResultCodeEnum.DATA_ERROR);
        }

    }
}
